// pages/discussion/discussion.ts
Page({

    /**
     * 页面的初始数据
     */
    data: {
      /*   navHeight: 0,
        menuButtonInfo: {
            bottom: 0,
            height: 0,
            top: 0,
            left: 0,
            right: 0,
            width: 0,
        }, */
        searchMarginTop: 0, // 搜索框上边距
        searchWidth: 0, // 搜索框宽度
        searchHeight: 0, // 搜索框高度
        // search_history: [''],
        search_value: '',
        // discussion_now: '华为P50鸿蒙手机',
        discussion_now: {},
        //星星点亮几颗
        star1: 1,
        star2: 1,
        star3: 1,
        star4: 1,
        content: '',
        //限制字数计算
        length_now: 0,
        //添加图片
        images_add: [],
        // images_add: ['../../pages/home/images/1.jpg', '../../pages/home/images/1.jpg', '../../pages/home/images/1.jpg'], 
        //删除图片
        x: 0,
        y: 0,
        h: 0,
        sure: false,
        add: true,

        //商品
        id: -1,
        url: '',
        up:false,
        img:'',

        //用户信息
        info:{},
        login: false,

        //内容
        text:'发表成功',
        type:'success'


    },
    back() {
        wx.navigateBack({
            delta: 1
        })
    },
    //星星改变
    change_star(event: any) {
        console.log(event.target.dataset)
        const target = event.target.dataset;

        switch (target.flag) {
            case '1':
                //产品推荐星星
                this.setData({ star1: target.star + 1 });
                break;
            case '2':
                this.setData({ star2: target.star + 1 });
                break;
            case '3':
                this.setData({ star3: target.star + 1 });
                break;
            case '4':
                this.setData({ star4: target.star + 1 });
                break;
        }
        console.log(this.data.star1)
    },
    //预览图片 传图片数组和当前点开的图片
    open(event: any) {
        const index=event.target.dataset.index
        // images_add
      /*   wx.navigateTo({
            url: `/pages/preview/preview?images=${JSON.stringify(this.data.images_add)}&now=${event.target.dataset.index}`

        }) */
        let per_images:string[]=[];
        for (let item of this.data.images_add) {
            const tt=this.data.url+item;
            item=tt as never
            console.log(item)
            per_images.push(item)
        }
        console.log(index)
        const now_image=this.data.url+per_images[index]
        console.log(now_image)
        wx.previewImage({
            current:per_images[index],
            urls: per_images // 需要预览的图片http链接列表
          })
    },
    upload() {
        const that = this
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                console.log(tempFilePaths)
                    wx.uploadFile({
                        url: `${that.data.url}api/uploadPicture`, //仅为示例，非真实的接口地址
                        filePath: tempFilePaths[0],
                        name: 'file',
                        formData: {
                            'user': 'test'
                        },
                        success(res: { data: { code: number, msg: string, data: string } }) {
                            console.log(res.data)
                            let info = JSON.parse(JSON.stringify(res.data));
                            info = JSON.parse(info)
                            if (info.code == 0) {
                                that.data.images_add.push(info.data)
                                that.setData({
                                    images_add: that.data.images_add
                                })
                                console.log(that.data.images_add)
                                //判断是否够九张图
                                console.log(that.data.images_add.length)
                                if (that.data.images_add.length == 9) {
                                    that.setData({
                                        add: false
                                    })
                                } else {
                                    that.setData({
                                        add: true
                                    })
                                }
                            }
                            //do something
                        }
                    })
                

            }
        })
    },
    ok(event: any) {
        console.log(event.detail.value)
        this.setData({
            content: event.detail.value
        })
    },
    up() {
        wx.showLoading({
            title: '发表中...',
          })
          
        const that=this;
        const data={
            id:+this.data.id,
            username:this.data.info.name,
            product_name:this.data.discussion_now.name,
            content:this.data.content,
            Score:this.data.star1,
            appearance:this.data.star2,
            performance:this.data.star3,
            price:this.data.star4,
            ImgUrl:this.data.images_add,
            portrait:this.data.info.img,
            openid:this.data.info.openid,
            tupian:this.data.img
        }
        console.log(data)
        wx.request({
            url: `${this.data.url}api/comment`, //仅为示例，并非真实的接口地址
            method:'POST',
            data,
            success(res: { data: { code: 0, msg:string,data: { data: { id: number, news_image: string, news_name: string }[]}[] } }) {
              console.log(res);
              if(res.data.code==0){
                  that.setData({
                    up:true,
                    text:'发表成功',
                    type:'success'
                  })
                  wx.hideLoading()
                  setTimeout(function(){
                    that.setData({
                        up:false
                      })
                      wx.navigateBack({
                        delta: 1 
                    })
                  },1000)
              }else{
                  console.log(444444444)
                that.setData({
                    up:true,
                    text:'缺少参数',
                    type:'warn'
                  })
                  wx.hideLoading()
                  setTimeout(function(){
                    that.setData({
                        up:false
                      })
                  },1000)
              }
      
            }
          })
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (option: { id: number,img:string }) {
        console.log(option.id)
        console.log(option.img)
        var app = getApp();
        const that = this;
        this.setData({
            url: app.globalData.url,
            id: option.id,
            info:app.globalData.info,
            login:app.globalData.status,
            img:option.img

        })
        wx.request({
            url: `${app.globalData.url}api/product?id=${option.id}`, //仅为示例，并非真实的接口地址
            success(res: { data: { code: 0, data: { data: { id: number, news_image: string, news_name: string }[] }[] } }) {
                console.log(res);
                const info = res.data.data;
                console.log(info)
                that.setData({
                    discussion_now: info[0]
                })

            }
        })
    },

    //拖拽删除
    change(event: any) {

        if (event.detail.y > 10) {
            this.setData({
                h: event.detail.y,
                del: true
            })
        } else {
            this.setData({
                del: false
            })
        }
        if (this.data.h > 200) {
            this.setData({
                sure: true
            })
        } else {
            this.setData({
                sure: false,
            })
        }
    },
    del(event: any) {
        if (this.data.h > 240) {
            this.data.images_add.splice(event.target.dataset.index, 1)
            this.setData({
                sure: false,
                del: false,
                images_add: this.data.images_add
            })

        } else {
            this.setData({
                x: 0,
                Y: 0,

            })
        }

    },
 //授权登录
 out() {
    const that = this
    wx.showModal({
        title: '提示',
        content: '是否授权登录',
        success(res) {
            if (res.confirm) {
                var app = getApp();
                app.login().then((res:number)=>{
                    console.log(res)
                    that.setData({
                        info: app.globalData.info,
                        url: app.globalData.url,
                        status: app.globalData.status,
                        login: true
                    })
                });
            } else if (res.cancel) {
                console.log('用户点击取消')
            }
        }
    })
},
})